package command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import User_impl.AddCommand;
import User_impl.DeleteCommand;
import User_impl.HelpCommand;
import User_impl.ListCommand;
import User_impl.LoginCommand;
import User_impl.LogoutCommand;
import User_impl.ResetCommand;
import User_impl.UpdateCommand;
import pojo.User;

public class UserCommandFactory extends SystemCommandFactory {

	private User user;

	public UserCommandFactory(User user) {
		this.user = user;
	}

	private static Map<CommandCode, Class<? extends Command>> commandMap = new HashMap<>();

	static {
		commandMap.put(CommandCode.HELP, HelpCommand.class);
		commandMap.put(CommandCode.UPDATE, UpdateCommand.class);
		commandMap.put(CommandCode.ADD, AddCommand.class);
		commandMap.put(CommandCode.DELETE, DeleteCommand.class);
		commandMap.put(CommandCode.LOGOUT, LogoutCommand.class);
		commandMap.put(CommandCode.RESET, ResetCommand.class);
		commandMap.put(CommandCode.LOGIN, LoginCommand.class);
		commandMap.put(CommandCode.LIST, ListCommand.class);
	}

	public Command buildCommand(CommandCode cmd) {
		Class<? extends Command> clazz = commandMap.get(cmd);

		if (clazz == null) {
			return super.buildCommand(cmd);
		}
		try {
			Constructor<? extends Command> c = clazz.getConstructor(User.class);
			Command command = c.newInstance(user);
			return command;
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

}
